//
//  XGSettingTableViewCell.swift
//  TodayNews
//
//  Created by monkey on 2020/2/8.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import SwiftTheme

class XGSettingTableViewCell: UITableViewCell, XGRegisterReusableViewProtocol
{
    /// 数据模型
     var itemModel: XGSettingItemModel? {
        didSet {
            titleLabel.text = itemModel?.title
            subTitleLabel.text = itemModel?.subTitle
            subTitleLabel.isHidden = itemModel?.subTitle == nil
            switch itemModel?.rightViewType {
            case .titleButton:
                rightTitleButton.setTitle(itemModel?.rightTitle, for: .normal)
                rightTitleButton.isHidden = false
                switchView.isHidden = true
                arrowImageView.isHidden = true
            case .switchView:
                switchView.isHidden = false
                rightTitleButton.isHidden = true
                arrowImageView.isHidden = true
            case .arrowImageView:
                arrowImageView.isHidden = false
                switchView.isHidden = true
                rightTitleButton.isHidden = true
            default:
                break
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载
    
    /// 标题
    private lazy var titleLabel: UILabel = UILabel(text: "测试", textColor: UIColor.darkGray, fontSize: 17, textAlignment: .left)
    /// 右标题
    private lazy var rightTitleButton: UIButton = UIButton(title: "测试", fontSize: 15, normalColor: UIColor.lightGray, target: nil, action: nil)
    /// 子标题
    private lazy var subTitleLabel: UILabel = UILabel(text: "测试", textColor: UIColor.red, fontSize: 13, textAlignment: .left)
    /// 开关
    private lazy var switchView: UISwitch = UISwitch()
    /// 箭头
    private lazy var arrowImageView: UIImageView = UIImageView(image: UIImage(named: "setting_rightarrow_8x14_"))
}

// MARK: - 私有扩展

private extension XGSettingTableViewCell
{
    /// 设置界面
    func setUpUI() -> Void
    {
        theme_backgroundColor = "colors.cell_background"
        titleLabel.theme_textColor = "colors.cell_title"
        
        contentView.addSubview(titleLabel)
        contentView.addSubview(rightTitleButton)
        contentView.addSubview(subTitleLabel)
        contentView.addSubview(switchView)
        contentView.addSubview(arrowImageView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(10)
        }
        
        rightTitleButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
        }
        
        switchView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
        }
        
        subTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.right.lessThanOrEqualTo(switchView.snp.left).offset(-10)
            make.bottom.equalToSuperview().offset(-5)
        }
    }
}
